package com.pan.cuiniubi.create.dao;

import com.pan.cuiniubi.entity.*;
import com.pan.cuiniubi.enumn.LaminaEnum;
import com.pan.cuiniubi.enumn.OperationEnum;
import com.pan.cuiniubi.utils.ReadYamlTool;

import java.util.ArrayList;
import java.util.List;

public class CreateInsertDaoMapping extends CreateDaoMappingImpl {
    @Override
    public List<MethodInfo> getMethodInfo(Table table) {
        MethodInfo methodInfo = new MethodInfo();
        Member member = new Member();
        member.setMemberName(table.getTableName())
                            .setIsPrimary(false).setMemberType(Object.class);
        methodInfo.setMethodName("insert"+table.getTableName())
                .setReturnStr(Integer.class.getName())
                .setParameterList(member);
        List<MethodInfo> result = new ArrayList<>();
        result.add(methodInfo);
        return result;
    }

    @Override
    public DaoInfo getDaoInfo(Table table) {
        DaoInfo daoInfo = new DaoInfo();

        String className = table.getTableName()+"InsertDao";
        daoInfo.setClassName(className)
                .setImportStr(getImportString(table))
                .setPackageStr(getPackageStr())
                .setMethodInfoList(getMethodInfo(table));
        return daoInfo;
    }

    public String getMethodLineStr(MethodInfo methodInfo){
        String path = (String) ReadYamlTool.getInstance().getMapLaminaValue(LaminaEnum.entity,"path");
        String parameterName = methodInfo.getParameterList().get(0).getMemberName();
        String typeName =  path + "."+parameterName.substring(0,1).toUpperCase()+parameterName.substring(1);
        String argsStr = methodInfo.getParameterList().stream()
                .map(p->typeName+" "+p.getMemberName().substring(0,1).toLowerCase()+
                        p.getMemberName().substring(1))
                .reduce((x,y)->x+", "+y).get();
        String result = "\t"+methodInfo.getReturnStr()+" "+methodInfo.getMethodName()+"("+argsStr+");\n";
        return result;
    }

    @Override
    public String getDaoContent(DaoInfo daoInfo) {

        String packageString = daoInfo.getPackageStr();
        String importString = daoInfo.getImportStr();
        String className = daoInfo.getClassName();
        String methodStr = daoInfo.getMethodInfoList().stream().map(this::getMethodLineStr)
                .reduce((a,b)->a+b).get();
        String result=daoTemplate;
        result = result.replace("(0)",className)
                .replace("(1)",methodStr);
        result = packageString +"\n"+ importString +"\n"+ result;
        return result;
    }



    public List<SqlInfo> getSqlInfo(Table table){
        String tableName =  table.getTableName();
        String path = (String) ReadYamlTool.getInstance().getMapLaminaValue(LaminaEnum.entity,"path");
        List<Member> memberList = table.getMemberList();

        String qian = memberList.stream().filter(member->!member.getIsPrimary())
                .map(member -> member.getMemberName())
                .reduce((x,y)->x+", "+y).get();
        String hou = memberList.stream().filter(member->!member.getIsPrimary())
                .map(member -> "#{"+member.getMemberName()+"}")
                .reduce((x,y)->x+", "+y).get();
        SqlInfo sqlInfo = new SqlInfo();
        sqlInfo.setId("insert");
        sqlInfo.setOpt(OperationEnum.insert.name());
        sqlInfo.setSql("insert into "+table.getTableName()+"("+qian+") "+"values ("+hou+");");
        List<SqlInfo> sqlInfoList = new ArrayList<>();
        sqlInfoList.add(sqlInfo);
        return sqlInfoList;
    }

    public MappingInfo getMappingInfo(Table table){
        MappingInfo mappingInfo = new MappingInfo();
        mappingInfo.setNamespace(getNamespace(table.getTableName(),OperationEnum.insert));
        mappingInfo.setSqlInfoList(getSqlInfo(table));
        String className = getClassName(table.getTableName(),OperationEnum.insert);
        mappingInfo.setClassName(className);
        return mappingInfo;
    }
}
